package com.jiyun.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.entity.Program;
import com.jiyun.service.IProgramService;
import com.jiyun.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-02-22
 */
@RestController
@RequestMapping("/program")
public class ProgramController {

    @Autowired
    IProgramService programService;

    @Autowired
    JwtUtil jwtUtil;

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public void addProgram(@RequestBody Program program,@RequestHeader("token") String token){

        program.setShenheState("审核中");
        program.setCreateTime(new Date());

        Claims claims = jwtUtil.parseJWT(token);
        LinkedHashMap user = claims.get("user", LinkedHashMap.class);
        program.setCreateUsername(user.get("username").toString());
        programService.save(program);
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateProgram(@RequestBody Program program){
        programService.updateById(program);
    }

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id){
        programService.removeById(id);
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        programService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public Program findById(@RequestParam Integer id){
        return programService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<Program> findAll(){
        return programService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<Program> findPage(@RequestParam Integer page,@RequestParam Integer pageSize){
        Page<Program> programPage = new Page<>(page, pageSize);
        return programService.page(programPage);
    }

}
